﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;


public partial class admin_AddAttendanceInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 79)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Login.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if (!IsPostBack)
        {
            AttendanceTime attendanceTime = AttendanceTimeManager.showIt(Server.MapPath(@"~/companyFiles/AttendanceTime.xml"));
            lblBackAM.Text = attendanceTime.BackAM;
            lblBackPM.Text = attendanceTime.BackPM;
            lblComeAM.Text = attendanceTime.ComeAM;
            lblComePM.Text = attendanceTime.ComePM;
            if (Session["user"] != null)
            {
                TarreOA.TarreOAModels.User user = (User)Session["user"];
                AttendanceInfo attendanceInfo = AttendanceInfoManager.FindAttendanceInfoByUser(user);
                if (attendanceInfo.AttendanceComeAM != null && attendanceInfo.AttendanceComeAM.ToShortDateString() != "0001/1/1")
                {
                    DateTime checkTime = Convert.ToDateTime(attendanceTime.ComeAM);
                    DateTime datenow = System.DateTime.Now;
                    DateTime dateTime = new DateTime(datenow.Year, datenow.Month, datenow.Day, checkTime.Hour, checkTime.Minute, 0);
                    this.AttendanceTime1.Text = attendanceInfo.AttendanceComeAM.ToShortTimeString() + (attendanceInfo.AttendanceComeAM > dateTime ? "迟到" : "正常");
                    if (attendanceInfo.AttendanceComeAMRemark != null && attendanceInfo.AttendanceComeAMRemark.Length > 20)
                    {
                        this.AttendanceRemark1.Text = attendanceInfo.AttendanceComeAMRemark.Substring(0, 20) + "...";
                    }
                    else
                    {
                        this.AttendanceRemark1.Text = attendanceInfo.AttendanceComeAMRemark;
                    }
                    this.ImageButton1.Visible = false;
                }
                if (attendanceInfo.AttendanceBackAM != null && attendanceInfo.AttendanceBackAM.ToShortDateString() != "0001/1/1")
                {
                    DateTime checkTime = Convert.ToDateTime(attendanceTime.BackAM);
                    DateTime datenow = System.DateTime.Now;
                    DateTime dateTime = new DateTime(datenow.Year, datenow.Month, datenow.Day, checkTime.Hour, checkTime.Minute, 0);
                    this.AttendanceTime2.Text = attendanceInfo.AttendanceBackAM.ToShortTimeString() + (attendanceInfo.AttendanceBackAM > dateTime ? "正常" : "早退");
                    if (attendanceInfo.AttendanceBackAMRemark != null && attendanceInfo.AttendanceBackAMRemark.Length > 20)
                    {
                        this.AttendanceRemark2.Text = attendanceInfo.AttendanceBackAMRemark.Substring(0, 20) + "...";
                    }
                    else
                    {
                        this.AttendanceRemark2.Text = attendanceInfo.AttendanceBackAMRemark;
                    }
                    this.ImageButton2.Visible = false;
                }
                if (attendanceInfo.AttendanceComePM != null && attendanceInfo.AttendanceComePM.ToShortDateString() != "0001/1/1")
                {
                    DateTime checkTime = Convert.ToDateTime(attendanceTime.ComePM);
                    DateTime datenow = System.DateTime.Now;
                    DateTime dateTime = new DateTime(datenow.Year, datenow.Month, datenow.Day, checkTime.Hour, checkTime.Minute, 0);
                    this.AttendanceTime3.Text = attendanceInfo.AttendanceComePM.ToShortTimeString() + (attendanceInfo.AttendanceComePM > dateTime ? "迟到" : "正常");
                    if (attendanceInfo.AttendanceComePMRemark != null && attendanceInfo.AttendanceComePMRemark.Length > 20)
                    {
                        this.AttendanceRemark3.Text = attendanceInfo.AttendanceComePMRemark.Substring(0, 20) + "...";
                    }
                    else
                    {
                        this.AttendanceRemark3.Text = attendanceInfo.AttendanceComePMRemark;
                    }
                    this.ImageButton3.Visible = false;
                }
                if (attendanceInfo.AttendanceBackPM != null && attendanceInfo.AttendanceBackPM.ToShortDateString() != "0001/1/1")
                {
                    DateTime checkTime = Convert.ToDateTime(attendanceTime.BackPM);
                    DateTime datenow = System.DateTime.Now;
                    DateTime dateTime = new DateTime(datenow.Year, datenow.Month, datenow.Day, checkTime.Hour, checkTime.Minute, 0);
                    this.AttendanceTime4.Text = attendanceInfo.AttendanceBackPM.ToShortTimeString() + (attendanceInfo.AttendanceBackPM > dateTime ? "正常" : "早退");
                    if (attendanceInfo.AttendanceComePMRemark != null && attendanceInfo.AttendanceBackPMRemark.Length > 20)
                    {
                        this.AttendanceRemark4.Text = attendanceInfo.AttendanceBackPMRemark.Substring(0, 20) + "...";
                    }
                    else
                    {
                        this.AttendanceRemark4.Text = attendanceInfo.AttendanceBackPMRemark;
                    }
                    this.ImageButton4.Visible = false;
                }
            }
            if (this.ImageButton1.Visible)
            {
                this.ImageButton2.Visible = false;
                this.ImageButton3.Visible = false;
                this.ImageButton4.Visible = false;
            }
            else if (this.ImageButton2.Visible)
            {
                this.ImageButton1.Visible = false;
                this.ImageButton3.Visible = false;
                this.ImageButton4.Visible = false;
            }
            else if (this.ImageButton3.Visible)
            {
                this.ImageButton1.Visible = false;
                this.ImageButton2.Visible = false;
                this.ImageButton4.Visible = false;
            }
            else if (this.ImageButton4.Visible)
            {
                this.ImageButton1.Visible = false;
                this.ImageButton2.Visible = false;
                this.ImageButton3.Visible = false;
            }
        }
    }
}
